package com.sqsf.controller;

import com.sqsf.javabean.User;
import com.sqsf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String login(HttpSession session){
        if(session.getAttribute("user")!=null){
            return "main";
        }

        return "redirect:/login.jsp";
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(HttpSession session, String username, String password){
        User login = userService.login(username, password);
        if(login==null){
            return "redirect:/login.jsp";
        }else{
            session.setAttribute("user",login);
            return "main";
        }
    }

    //退出登录
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();//销毁会话信息
        return "redirect:/login.jsp";
    }
}
